from datetime import date 
ValidEvents = {
"az":"Arizona",
"or":"Autodesk Oregon",
"nh":"BAE Granite State",
"la":"Bayou",
"in":"Boilermaker",
"ma":"Boston",
"oh":"Buckeye",
"md":"Chesapeake",
"co":"Colorado",
"ct":"Connecticut",
"da":"Dallas",
"roc":"Finger Lakes",
"fl":"Florida",
"kc":"Greater Kansas City",
"on":"Greater Toronto",
"hi":"Hawaii",
"is":"Israel",
"nv":"Las Vegas",
"tx":"Lone Star",
"ca":"Los Angeles",
"wa":"Microsoft Seattle",
"il":"Midwest",
"mn":"Minnesota 10000 Lakes",
"mn2":"Minnesota North Star",
"nj":"New Jersey",
"ny":"New York City",
"nc":"North Carolina",
"ok":"Oklahoma",
"sc":"Palmetto",
"ga":"Peachtree",
"pa":"Philadelphia",
"pit":"Pittsburgh",
"sac":"Sacramento",
"sdc":"San Diego",
"li":"SBPLI Long Island",
"sj":"Silicon Valley",
"mo":"St. Louis",
"ut":"Utah",
"va":"Virginia",
"dc":"Washington DC",
"wat":"Waterloo",
"wi":"Wisconsin",
"wor":"WPI",
"wc":"Ann Arbor MI District",
"dt1":"Cass Tech MI District",
"dt":"Detroit MI District",
"gg":"Kettering University MI District",
"gt":"Traverse City MI District",
"oc":"Troy MI District",
"mi":"West Michigan MI District",
"gl":"Michigan State Championship",
"cmp":"FIRST Championship",
"arc":"FIRST Championship - Archimedes Division",
"cur":"FIRST Championship - Curie Division",
"gal":"FIRST Championship - Galileo Division",
"new":"FIRST Championship - Newton Division",
"ein":"FIRST Championship - Einstein Field",
"az":"az",
"or":"or",
"nh":"nh",
"la":"la",
"in":"in",
"ma":"ma",
"oh":"oh",
"md":"md",
"co":"co",
"ct":"ct",
"da":"da",
"roc":"roc",
"fl":"fl",
"kc":"kc",
"on":"on",
"hi":"hi",
"is":"is",
"nv":"nv",
"tx":"tx",
"ca":"ca",
"wa":"wa",
"il":"il",
"mn":"mn",
"mn2":"mn2",
"nj":"nj",
"ny":"ny",
"nc":"nc",
"ok":"ok",
"sc":"sc",
"ga":"ga",
"pa":"pa",
"pit":"pit",
"sac":"sac",
"sdc":"sdc",
"li":"li",
"sj":"sj",
"mo":"mo",
"ut":"ut",
"va":"va",
"dc":"dc",
"wat":"wat",
"wi":"wi",
"wor":"wor",
"wc":"wc",
"dt1":"dt1",
"dt":"dt",
"gg":"gg",
"gt":"gt",
"oc":"oc",
"mi":"mi"
}
ValidWhat = {
"rankings":False,"awards":False,"schedule":True,"results":True,"team_list":False,"team_info":False,"team_history":False
}
Format = {
"xml":"Formats/XML/%s.xml",
"json":"Formats/JSON/%s.xml",
"human":"Formats/Human/%s.xml"
}
Years = ["2010","2009","2008","2007"]
When = ["qualification","elimination"]
class URLParser:
	def __init__(self,url):
		self.URLParts = url.lower().strip().split("/")
		self.What = None
		self.When = None
		self.Year = date.today().year
		self.Top = None
		self.Bottom = None
		self.Format = "xml"
		self.FormatPath = None
		self.Event = None
		self.Error = []
		self.TeamNo = None
	
	def Parse(self):
		RequireTeamNo = None
		RequireWhen = None
		
		for Part in self.URLParts:
		
			if Part in Years:
				self.Year = int(Part)
			
			if Part in Format:
				self.Format = Part
				Part = ""
			
			if Part in ValidWhat:
				RequireWhen = (ValidWhat[Part] and self.When == None)
				self.What = Part
				if Part == "team_history":
					RequireTeamNo = self.TeamNo == None
				Part = ""
			
			if Part in ValidEvents:
				self.Event = Part
				Part = ""
		
			if Part in When:
				self.When = Part
				RequireWhen = False
				Part = ""
				
			if len(Part.split("-")) == 2:
				splits = Part.split("-")
				if splits[0].isdigit():
					self.Top = int(splits[0])-1
				if splits[1].isdigit():
					self.Bottom = int(splits[1])
				Part = ""
			if Part in ValidEvents:
				self.Event = ValidEvents[Part]
				Part = ""
			if Part.isdigit():
				self.TeamNo = int(Part)
				RequireTeamNo = False		
		if self.What == None:
			self.Error.append("Please specify what you want.\n")
		if RequireTeamNo:
			self.Error.append("Please specify a team number.\n")
		if RequireWhen:
			self.Error.append("Please specify either Elimination or Qualification.\n")

	def GetFormatPath(self):
		if not ValidWhat[self.What]:
			self.FormatPath = Format[self.Format]%(self.What)
		else:
			self.FormatPath = Format[self.Format]%("%s_%s"%(self.When,self.What))
		return self.FormatPath
		
	def GetFormatTemplate(self):
		return Format[self.Format]
	
	def Hash(self):
		return hash("%s-%s-%s-%s-%s"%(self.Year,self.Event,self.What,self.When,self.TeamNo))
